extends Node

var chara=false#It's me, my best partner=)
var splash=preload("res://scenes/splash.tscn").instance()
var main_menu=preload("res://scenes/ui.tscn").instance()
var settings=preload("res://scenes/settings.tscn").instance()
var about=preload("res://scenes/about.tscn").instance()
var story_s=preload("res://scenes/story.tscn").instance()
var clock=preload("res://scenes/frags/clock.tscn").instance()
var gaming=preload("res://scenes/gaming.tscn").instance()
var l2d=preload("res://scenes/frags/l2d.tscn").instance()
var item=[
	[load("res://_icon.png"),"此处空无一物。"],
	[load("res://_icon.png"),"一把光洁如新的手电筒。"],
	[load("res://_icon.png"),"一把很结实的长伞。"],
	[load("res://_icon.png"),"一把普通的折叠伞。"]
]
enum items{
	nothing=0,
	flashlight=1,
	umbrella_long=2,
	umbrella_short=3
}
var pointer_mover=Tween.new()#移动pointer的Tween
var jump_splash=false#是否跳过splash
var cur_sce=0#当前场景0主界面 1设置 2关于 3讲故事 4游戏场景
enum scene{
	main=0,
	settings=1,
	about=2,
	story=3,
	gaming=4
}
var snd=AudioStreamPlayer2D.new()#2D声音播放器
var first=true#是否有存档运行
var settings_exitable=true#是否可以从settings退回主界面
var if_story=false#是否讲过故事
var changing_scene=false
var sms0=Tween.new()#用于移动ui
var sms1=Tween.new()#用于黑屏splash/主Camera动画
var the_scene_that_going_to_be_removed#顾名思义
var the_scene_name_that_going_to_change_the_current_scene_name_to#~
onready var dcmd=$"/root/Node2D_0/Camera2D_0/tdebug/debug"
onready var camera=$"/root/Node2D_0/Camera2D_0"
onready var phone_ctrl=$"/root/Node2D_0/Camera2D_0/phone_ctrl"
onready var cm=$"/root/Node2D_0/cm"
onready var pointer=$"/root/Node2D_0/Camera2D_0/Control/pointer"
onready var mask=$"/root/Node2D_0/Camera2D_0/mask"
onready var alert_text=$"/root/Node2D_0/Camera2D_0/alert/text"
onready var alert_head=$"/root/Node2D_0/Camera2D_0/alert/head"
onready var alert_y=$"/root/Node2D_0/Camera2D_0/alert/y"
onready var alert_n=$"/root/Node2D_0/Camera2D_0/alert/n"
onready var pause_panel=$"/root/Node2D_0/Camera2D_0/Control/pause_panel"
var smcr=false
var if_inani=true#是否播放开始动画
var if_user_agrees=false
signal user_has_made_their_decision

func _ready():
	pause_mode=Node.PAUSE_MODE_PROCESS
	randomize()
	d.init()
	d.dlgbox.bbcode_text=""
	add_child(sms0)
	add_child(sms1)
	load_settings()
	chara={
		"sys0":load("res://data/images/sys0.png"),
		"icon":load("res://icon.png"),
		"R0":load("res://data/images/heads/R.png")
	}
	add_child(pointer_mover)
	add_child(snd)
	snd.position=Vector2(512,256)
	if(!jump_splash):
		add_child(splash)
		yield(get_tree().create_timer(8),"timeout")
		$"splash/ap".play("leaf_splash_text")
		yield(get_tree().create_timer(8),"timeout")
		$"splash/ap".play("leaf_splash_full_screen")
		yield(get_tree().create_timer(1),"timeout")
		add_child(gaming)
		$"gaming".rotation_degrees=180
		$"gaming".position=Vector2(512,1300)
		$"gaming/mcr".movable=false
		add_child(main_menu)
		$"ui".rect_position=Vector2(0,0)
		$"splash/Control/RichTextLabel".visible=false
		yield(get_tree().create_timer(1),"timeout")
		remove_child(splash)
	else:
		add_child(gaming)
		add_child(main_menu)
		$"gaming".rotation_degrees=180
		$"gaming".position=Vector2(512,1300)
		$"gaming/mcr".movable=false
		sms0.interpolate_property($"ui","rect_position",Vector2(1024,0),Vector2(0,0),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		sms0.start()
#	d.speak([
#		[chara["sys0"],[
#			["[shake rate=16 level=10][fade start=0 length=128]testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest_/w3text0/w0测试文本/w1零[/fade][/shake]",0.05,true],
#			["test/w3_text1//测试/f0文本一",0.05,true]
#		]]],
#		[funcref(self,"f0"),funcref(self,"f1")])

#func f0(r):
#	print("f0")
#	r.playback_speed=1
#
#func f1(r):
#	print("f1")
#	r.playback_speed=1

func dgo(t,if_add=false):
	if(if_add):
		dcmd.bbcode_text+=str(t)
	else:
		dcmd.bbcode_text=str(t)

func save():
	var dir=Directory.new()
	var config=ConfigFile.new()
	config.set_value("gaming","if_inani",if_inani)
	config.set_value("gaming","mcr_position",smcr.position)
	config.set_value("gaming","if_bag",smcr.if_bag)
	config.set_value("gaming","items",smcr.items)
	var file=File.new()
	if(file.file_exists(OS.get_executable_path().get_base_dir()+"/_local_")):
		if(!dir.dir_exists(OS.get_executable_path().get_base_dir()+"/fallen")):
			dir.make_dir_recursive(OS.get_executable_path().get_base_dir()+"/fallen")
		config.save(OS.get_executable_path().get_base_dir()+"/fallen/save.cfg")
	else:
		config.save("user://save.cfg")
	file.close()

func gload():
	var config=ConfigFile.new()
	var file=File.new()
	var err
	if(file.file_exists(OS.get_executable_path().get_base_dir()+"/_local_")):
		err=config.load(OS.get_executable_path().get_base_dir()+"/fallen/save.cfg")
	else:
		err=config.load("user://save.cfg")
	if(err!=OK):
		return
	first=false
	file.close()
	for section in config.get_sections():
		match section:
			"gaming":
				if_inani=config.get_value(section,"if_inani",false)
				smcr.position=config.get_value(section,"mcr_position",Vector2(0,0))
				smcr.if_bag=config.get_value(section,"if_bag",false)
				smcr.items=config.get_value(section,"items",[])
			_:
				pass

func save_settings():
	var dir=Directory.new()
	var config=ConfigFile.new()
	config.set_value("settings","debug",i.keys.debug)
	config.set_value("settings","jump_splash",jump_splash)
	config.set_value("settings","dialogue_font_size",d.font.size)
	config.set_value("settings","if_phone_ctrl",phone_ctrl.is_phone_ctrl)
	config.set_value("story","if_story",if_story)
	var file=File.new()
	if(file.file_exists(OS.get_executable_path().get_base_dir()+"/_local_")):
		if(!dir.dir_exists(OS.get_executable_path().get_base_dir()+"/fallen")):
			dir.make_dir_recursive(OS.get_executable_path().get_base_dir()+"/fallen")
		config.save(OS.get_executable_path().get_base_dir()+"/fallen/settings.cfg")
	else:
		config.save("user://settings.cfg")
	file.close()

func load_settings():
	var config=ConfigFile.new()
	var file=File.new()
	var err
	if(file.file_exists(OS.get_executable_path().get_base_dir()+"/_local_")):
		err=config.load(OS.get_executable_path().get_base_dir()+"/fallen/settings.cfg")
	else:
		err=config.load("user://settings.cfg")
	if(err!=OK):
		return
	first=false
	file.close()
	for section in config.get_sections():
		match section:
			"settings":
				i.keys.debug=config.get_value(section,"debug",false)
				jump_splash=config.get_value(section,"jump_splash",false)
				d.resize_font(config.get_value(section,"dialogue_font_size",16))
				phone_ctrl.is_phone_ctrl=config.get_value(section,"if_phone_ctrl",false)
				phone_ctrl.visible=config.get_value(section,"if_phone_ctrl",false)
			"story":
				if_story=config.get_value(section,"if_story",false)
			_:
				pass

func start():
	changing_scene=true
	if(sms0.is_connected("tween_all_completed",self,"changed_scene")):
		sms0.disconnect("tween_all_completed",self,"changed_scene")
	sms0.remove_all()
	pointer_mover.remove_all()
	pointer.rect_position=Vector2(0,-64)
	pointer.rect_rotation=90
	sms0.interpolate_property($"ui","rect_position",Vector2(0,0),Vector2(-1024,0),0.5,Tween.TRANS_LINEAR)
	sms0.start()
	yield(sms0,"tween_all_completed")
	remove_child(main_menu)
	smcr=$"gaming/mcr"
	gload()
	if smcr.items.has([items.umbrella_long,1])&&$"gaming".has_node("umbrella_long"):
		$"gaming".remove_child($"gaming/umbrella_long")
	if smcr.if_bag&&$"gaming".has_node("bag"):
		$"gaming".remove_child($"gaming/bag")
	if if_inani:
		cur_sce=scene.gaming
		$"gaming".rotation_degrees=0
		$"gaming".position=Vector2(0,0)
		$"gaming".pa()
	else:
		$"gaming/Lamp/Light2D".hide()
		$"gaming/mask0".rect_position=Vector2(-512,0)
		$"gaming/mask0".show()
		sms1.interpolate_property(camera,"rotation_degrees",camera.rotation_degrees,180,0.5,Tween.TRANS_LINEAR)
		sms1.interpolate_property(camera,"position",camera.position,camera.position+Vector2(0,512),0.5,Tween.TRANS_LINEAR)
		sms1.start()
		yield(sms1,"tween_all_completed")
		$"gaming/Lamp2/Light2D".hide()
		$"gaming".rotation_degrees=0
		$"gaming".position=Vector2(0,0)
		$"gaming/mask0".rect_position=Vector2(smcr.position.x-512,512)
		camera.position.x=smcr.position.x
		camera.rotation_degrees=0
		cur_sce=scene.gaming
		smcr.movable=true
		$"gaming/Lamp/Light2D".show()
	changing_scene=false
	yield(get_tree().create_timer(1),"timeout")
	$"gaming/Lamp2/Light2D".show()
	$"gaming/mask0".hide()

func open_settings():
	change_scene("ui","settings",main_menu,settings,scene.settings)

func open_about():
	change_scene("ui","about",main_menu,about,scene.about)

func story():
	sms1.interpolate_property(mask,"color",Color(0,0,0,0),Color(0,0,0,1),0.5,Tween.TRANS_LINEAR,Tween.EASE_IN_OUT)
	sms1.start()
	yield(sms1,"tween_all_completed")
	remove_child(gaming)
	add_child(l2d)
	change_scene("about","story",about,story_s,scene.story)
	sms1.interpolate_property(mask,"color",Color(0,0,0,1),Color(0,0,0,0),0.5,Tween.TRANS_LINEAR,Tween.EASE_IN_OUT)
	sms1.start()

func exit_story():
	sms1.interpolate_property(mask,"color",Color(0,0,0,0),Color(0,0,0,1),0.5,Tween.TRANS_LINEAR,Tween.EASE_IN_OUT)
	sms1.start()
	yield(sms1,"tween_all_completed")
	add_child(gaming)
	remove_child(l2d)
	change_scene("story","about",story_s,about,scene.about)
	sms1.interpolate_property(mask,"color",Color(0,0,0,1),Color(0,0,0,0),0.5,Tween.TRANS_LINEAR,Tween.EASE_IN_OUT)
	sms1.start()

func ret_mainmenu():
	if(sms0.is_connected("tween_all_completed",self,"changed_scene")):
		sms0.disconnect("tween_all_completed",self,"changed_scene")
	sms0.remove_all()
	changing_scene=true
	cur_sce=scene.main
	smcr.movable=false
	camera.close_bag()
	$"gaming/mask0".rect_position=Vector2(camera.position.x-1024,512)
	$"gaming/Lamp/Light2D".hide()
	$"gaming/Lamp2/Light2D".hide()
	$"gaming/mask0".show()
	sms1.interpolate_property(camera,"rotation_degrees",0,180,0.5,Tween.TRANS_LINEAR)
	sms1.interpolate_property(camera,"position",camera.position,Vector2(camera.position.x,768),0.5,Tween.TRANS_LINEAR)
	sms1.start()
	yield(sms1,"tween_all_completed")
	camera.position=Vector2(512,768)
	$"gaming/mask0".rect_position=Vector2(-512,0)
	$"gaming".rotation_degrees=180
	$"gaming".position=Vector2(512,1300)
	pointer.rect_rotation=0
	camera.rotation_degrees=0
	add_child(main_menu)
	sms1.interpolate_property(camera,"position",Vector2(512,768),Vector2(512,256),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms1.interpolate_property($"ui","rect_position",Vector2(-1024,0),Vector2(0,0),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms1.start()
	yield(sms1,"tween_all_completed")
	$"gaming/mask0".hide()
	$"gaming/Lamp/Light2D".show()
	$"gaming/Lamp2/Light2D".show()
	pause_panel.can_show_panel=true
	changing_scene=false

func ret_settings():
	if sms0.is_connected("tween_all_completed",self,"changed_scene"):
		sms0.disconnect("tween_all_completed",self,"changed_scene")
	changing_scene=true
	cur_sce=scene.settings
	smcr.movable=false
	camera.close_bag()
	$"gaming/mask0".rect_position=Vector2(camera.position.x-1024,512)
	$"gaming/Lamp/Light2D".hide()
	$"gaming/Lamp2/Light2D".hide()
	$"gaming/mask0".show()
	sms1.interpolate_property(camera,"rotation_degrees",0,180,0.5,Tween.TRANS_LINEAR)
	sms1.interpolate_property(camera,"position",camera.position,Vector2(camera.position.x,768),0.5,Tween.TRANS_LINEAR)
	sms1.start()
	yield(sms1,"tween_all_completed")
	camera.position=Vector2(512,768)
	$"gaming/mask0".rect_position=Vector2(-512,0)
	$"gaming".rotation_degrees=180
	$"gaming".position=Vector2(512,1300)
	pointer.rect_rotation=0
	camera.rotation_degrees=0
	add_child(settings)
	sms1.interpolate_property(camera,"position",Vector2(512,768),Vector2(512,256),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms1.start()
	sms0.remove_all()
	sms0.interpolate_property($"settings","rect_position",Vector2(1024,0),Vector2(0,0),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.start()
	yield(sms1,"tween_all_completed")
	$"gaming/mask0".hide()
	$"gaming/Lamp/Light2D".show()
	$"gaming/Lamp2/Light2D".show()
	pause_panel.can_show_panel=true
	changing_scene=false

func _exit_tree():
	save_settings()

func _process(_delta):
	dcmd.visible=i.keys.debug
	if(i.keys.debug):
		dcmd.bbcode_text=String(i.keys)+"\ncurrent scene:"+String(cur_sce)+"\n"+"main_character.position:"+(String(smcr.position) if (smcr is KinematicBody2D) else "uninited")+"\ncamera.position:"+String(camera.position)
	if(i.keys.cancel&&!changing_scene):
		match cur_sce:
			scene.main:
				pass
			scene.settings:
				if(settings_exitable):
					change_scene("settings","ui",settings,main_menu,scene.main)
			scene.about:
				change_scene("about","ui",about,main_menu,scene.main)
			scene.story:
				pass
			scene.gaming:
				pass
			_:
				pass

func change_scene(s0a,s1a,s0,s1,s1n):
	changing_scene=true
	if(!s1.is_inside_tree()):
		add_child(s1)
	if(pointer_mover.is_active()):
		pointer_mover.stop_all()
		pointer_mover.remove_all()
	pointer_mover.interpolate_property(pointer,"rect_position",pointer.rect_position,Vector2(0,-64),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	pointer_mover.start()
	if(sms0.is_active()):
		sms0.seek(sms0.get_runtime())
		sms0.stop_all()
		sms0.remove_all()
	if(get_node(s0a) is Control):
		sms0.interpolate_property(get_node(s0a),"rect_position",Vector2(0,0),Vector2(-1024,0),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	elif(get_node(s0a) is Node2D):
		sms0.interpolate_property(get_node(s0a),"position",Vector2(0,0),Vector2(-1024,0),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	if(get_node(s1a) is Control):
		sms0.interpolate_property(get_node(s1a),"rect_position",Vector2(-1024,0),Vector2(0,0),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	elif(get_node(s1a) is Node2D):
		sms0.interpolate_property(get_node(s1a),"position",Vector2(-1024,0),Vector2(0,0),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	the_scene_that_going_to_be_removed=s0
	the_scene_name_that_going_to_change_the_current_scene_name_to=s1n
	sms0.start()
	if(!sms0.is_connected("tween_all_completed",self,"changed_scene")):
		sms0.connect("tween_all_completed",self,"changed_scene")

func changed_scene():
	remove_child(the_scene_that_going_to_be_removed)
	cur_sce=the_scene_name_that_going_to_change_the_current_scene_name_to
	changing_scene=false

func reset_button(node,pos):
	if(!changing_scene):
		pointer_mover.interpolate_property(pointer,"rect_position",pointer.rect_position,Vector2(pos.x-60,pos.y+16),0.2,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		pointer_mover.interpolate_property(node,"rect_position",node.rect_position,pos,0.2,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		pointer_mover.start()

func alert(t):
	alert_text.bbcode_text=t
	alert_y.mouse_filter=Control.MOUSE_FILTER_STOP
	alert_n.mouse_filter=Control.MOUSE_FILTER_STOP
	sms0.interpolate_property(alert_head,"rect_position",Vector2(-64,256),Vector2(150,256),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.interpolate_property(alert_y,"rect_position",Vector2(350,600),Vector2(350,340),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.interpolate_property(alert_n,"rect_position",Vector2(550,600),Vector2(550,340),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.interpolate_property(alert_text,"rect_position",Vector2(512,288),Vector2(256,256),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.interpolate_property(alert_text,"rect_size",Vector2(0,0),Vector2(512,64),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.start()
	yield(self,"user_has_made_their_decision")
	$"/root/Node2D_0/Camera2D_0/Control/pause_panel/sms".stop_all()
	$"/root/Node2D_0/Camera2D_0/Control/pause_panel/sms".remove_all()
	alert_y.mouse_filter=Control.MOUSE_FILTER_IGNORE
	alert_n.mouse_filter=Control.MOUSE_FILTER_IGNORE
	pointer.rect_position=Vector2(0,-64)
	sms0.interpolate_property(alert_head,"rect_position",Vector2(150,256),Vector2(-64,256),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.interpolate_property(alert_y,"rect_position",Vector2(350,340),Vector2(350,600),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.interpolate_property(alert_n,"rect_position",Vector2(550,340),Vector2(550,600),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.interpolate_property(alert_text,"rect_position",Vector2(256,256),Vector2(512,288),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.interpolate_property(alert_text,"rect_size",Vector2(512,64),Vector2(0,0),0.5,Tween.TRANS_CUBIC,Tween.EASE_OUT)
	sms0.start()
